﻿using log4net;
using Mangox.BLL.Applet;
using Mangox.BLL.Mangox;
using Mangox.Common;
using Mangox.Model.MangoxDB;
using MGXAPI.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;

namespace MGXAPI.Controllers.API
{
    /// <summary>
    /// 管理员操作类，反馈等
    /// mgx
    /// </summary>
    public class AdminController : ApiController
    {
        static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private static string className = (new StackTrace()).GetFrame(1).GetMethod().ReflectedType.Name + "/";

        /// <summary>
        /// 用户反馈
        /// http://localhost:13772/api/Admin/AddUserFeedback
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public bool AddUserFeedback(M_Feedback model)
        {
            AdminBLL bll = new AdminBLL();
            return bll.AddUserFeedback(model);
        }


        #region UserSign 签到

        /// <summary>
        /// 获取签到
        /// http://localhost:13772/api/Admin/GetUserSign?TokenStr=123
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultModel GetUserSign(string TokenStr)
        {
            ResultModel result = new ResultModel();
            result.Code = -1;

            try
            {
                #region 验证Token

                var token = MgxCommBLL.DecryptToken(TokenStr);
                if (token == null || !token.IsToken)
                {
                    result.Msg = "Token无效或过期";
                    return result;
                }
                result.TokenUpdate = MgxCommBLL.CreateToken(token.UserID.ToString(), token.MgxHashID);

                #endregion

                UserSign us = new UserSignBLL().GetUserSign(token.UserID);
                if (us != null)
                {
                    result.Code = 0;
                    //result.Msg = "获取成功";

                    //已经签到
                    if (Convert.ToDateTime(us.LastModifyTime).Date == DateTime.Now.Date)
                    {
                        result.Data = new { us.Count, IsSign = 1, us.SignCount, us.Integral };
                    }
                    else
                    {
                        result.Data = new { us.Count, IsSign = 0, us.SignCount, us.Integral };
                    }
                }
                else
                {
                    result.Code = 0;
                    result.Data = new { Count = 0, IsSign = 0 };
                    log.Info($"签到 第一次 UserID-{token.UserID}");
                }
            }
            catch (Exception ex)
            {
                result.Msg = "获取失败";
                log.Error(className + $"获取签到失败 Fail-{ex.ToString()}");
            }
            return result;
        }

        /// <summary>
        /// 添加、更新签到，如果用户不是第一次签到，就+1
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultModel SignIn(RequestModel p) //UserSign model
        {
            ResultModel result = new ResultModel();
            result.Code = -1;

            log.Info(className + $"签到 param-{JsonCommon.ToJson(p)}");

            try
            {
                #region 验证Token

                var token = MgxCommBLL.DecryptToken(p.Token);
                if (token == null || !token.IsToken)
                {
                    result.Msg = "Token无效或过期";
                    return result;
                }
                result.TokenUpdate = MgxCommBLL.CreateToken(token.UserID.ToString(), token.MgxHashID);

                #endregion

                UserSign us = new UserSignBLL().SignIn(new UserSign() { UserID = token.UserID });

                if (us != null)
                {
                    result.Code = 0;
                    result.Msg = "签到成功";
                    result.Data = new { us.Count, IsSign = 1, us.SignCount, us.Integral };
                }
                else
                {
                    result.Data = new { us.Count, IsSign = 0, us.SignCount, us.Integral };
                    result.Msg = "签到失败";
                }
            }
            catch (Exception ex)
            {
                log.Error(className + $"签到失败 Fail-{ex.ToString()}");
            }
            return result;
        }

        #endregion

    }
}
